﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using AMPLibrary;
using AMPLibrary.Media;
using AMPLibrary.Collections;

namespace MediaLibrary.Controls
{
    /// <summary>
    /// Interaction logic for NewPlaylistWindow.xaml
    /// </summary>
    public partial class NewPlaylistWindow : Window
    {
        private Collection<MediaFile> mediaCollection;

        public string PlaylistPath
        {
            get;
            set;
        }

        private NewPlaylistWindow()
        {
            InitializeComponent();
        }

        public NewPlaylistWindow(Collection<MediaFile> mediaCollection)
        {
            InitializeComponent();
            this.mediaCollection = mediaCollection;
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }

        private void createPlaylistButton_Click(object sender, RoutedEventArgs e)
        {
            if (playlistNameTextBox.Text == string.Empty)
                return;

            Playlist playlist = new Playlist();
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = MediaTypes.PlaylistTypeFilter;
            sfd.FileName = playlistNameTextBox.Text + ".m3u";

            ComboBoxItem cbi = (ComboBoxItem)playlistTypeComboBox.SelectedItem;

            if (cbi == null)
                return;
            
            string type = cbi.Content.ToString();

            if (type == "Save as...")
            {
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.DialogResult = true;
                    Playlist.SavePlaylist(sfd.FileName, mediaCollection);
                    this.PlaylistPath = sfd.FileName;
                    this.Close();
                }
                else
                {
                    this.DialogResult = false;
                    this.Close();
                }
            }
            else if (type == "Default Music Directory")
            {
                if (Settings.PlaylistFolderMusic == string.Empty)
                {
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        this.DialogResult = true;
                        Playlist.SavePlaylist(sfd.FileName, mediaCollection);
                        this.PlaylistPath = sfd.FileName;
                        this.Close();
                    }
                    else
                    {
                        this.DialogResult = false;
                        this.Close();
                    }
                }
                else
                {
                    this.PlaylistPath = Settings.PlaylistFolderMusic + "\\" + playlistNameTextBox.Text + ".m3u";
                    Playlist.SavePlaylist(Settings.PlaylistFolderMusic + "\\" + playlistNameTextBox.Text + ".m3u", mediaCollection);
                    this.Close();
                }
            }
            else if (type == "Default Video Directory")
            {
                if (Settings.PlaylistFolderVideo == string.Empty)
                {
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        this.DialogResult = true;
                        Playlist.SavePlaylist(sfd.FileName, mediaCollection);
                        this.PlaylistPath = sfd.FileName;
                        this.Close();
                    }
                    else
                    {
                        this.DialogResult = false;
                        this.Close();
                    }
                }
                else
                {
                    this.DialogResult = true;
                    this.PlaylistPath = Settings.PlaylistFolderVideo + "\\" + playlistNameTextBox.Text + ".m3u";
                    Playlist.SavePlaylist(Settings.PlaylistFolderVideo + "\\" + playlistNameTextBox.Text + ".m3u", mediaCollection);
                    this.Close();
                }
            }
        }
    }
}
